package com.bite.demo.controller;

import com.bite.demo.enums.ResultCodeEnum;
import com.bite.demo.model.BlogInfo;
import com.bite.demo.model.Result;
import com.bite.demo.model.UserInfo;
import com.bite.demo.service.UserService;
import com.bite.demo.utils.JwtUtils;
import com.bite.demo.utils.SecurityUtils;
import io.jsonwebtoken.Jwts;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.file.attribute.UserPrincipalNotFoundException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public Result login(String userName,String password) {
        log.info("用户账号：userName{}"+ userName);
        //验证账号密码是否正确
        Assert.hasLength(userName,"用户名不能为空");
        Assert.hasLength(password,"密码不能为空");
/*        if(!StringUtils.hasLength(userName) && !StringUtils.hasLength(password)) {
            return Result.fail(ResultCodeEnum.PARAM_INVAILD.getCode(), "账号或密码不能为空");
        }*/

        UserInfo userInfo = userService.selectUserInfoByName(userName);//这个方法就抛出异常了，所以这个里面写的有问题
        System.out.println();
        if(userInfo == null || userInfo.getId() == null) {
            log.error("用户验证失败");
            return Result.fail(ResultCodeEnum.PARAM_INVAILD.getCode(), "用户不存在");
        }
        //下面的才是验证你的密码，你都没走到这里
        if(!SecurityUtils.verify(password,userInfo.getPassword())) {
            log.error("密码验证失败");
            return Result.fail(ResultCodeEnum.PARAM_INVAILD.getCode(), "密码错误");
        }
        //验证一下密码
//        if(!password.equals(userInfo.getPassword())) {
//            return Result.fail(ResultCodeEnum.PARAM_INVAILD.getCode(), "密码错误");
//        }
        Map<String,Object> cliams = new HashMap<>();
        cliams.put("id",userInfo.getId());
        cliams.put("name",userInfo.getUserName());

        //生成令牌
        String jwt = JwtUtils.genJwt(cliams);
        return Result.success(jwt);
    }

    /**
     * 获取用户信息
     *
     */
    @RequestMapping("/getUserInfo")
    public UserInfo getUserInfo(HttpServletRequest request) {
        //从header获取token
        String userToken = request.getHeader("user_token_header");
        //从token获取id
        Integer userId = JwtUtils.getUserIdFromToken(userToken);
        Assert.notNull(userId,"用户未登录");
        UserInfo userInfo = userService.selectUserInfoById(userId);
        return userInfo;
    }

    /**
     * 获取博客信息
     *
     */
    @RequestMapping("/getAuthorInfo")
    public UserInfo getAuthorInfo(Integer blogId) {
        //根据博客Id 获取作者Id
        Assert.notNull(blogId,"博客Id不能为null");
        return userService.selectAuthorInfoByBlogId(blogId);
        //根据作者Id 获取作者信息
    }





}
